﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LiteFramework
{
    public class OprateResult : Bag
    {
        #region Ctor.
        public OprateResult()
        {
            this.Code = 0;
        }
        public OprateResult(bool result)
            : this()
        {
            this.Result = result;
        }
        public OprateResult(bool result, object returnValue)
            : this()
        {
            this.Result = result;
            this.ReturnValue = returnValue;
        }
        #endregion

        #region Property
        /// <summary>
        /// 错误码。0代表成功，否则失败
        /// </summary>
        public int Code { get; set; }
        /// <summary>
        /// 错误消息。
        /// </summary>
        public string Error { get; set; }
        /// <summary>
        /// 操作是否成功
        /// </summary>
        public bool Result { get; set; }
        /// <summary>
        /// 返回值
        /// </summary>
        public object ReturnValue { get; set; }
        #endregion

        #region Method
        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="result"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public static T ConvertReturnValue<T>(OprateResult result, T defaultValue)
        {
            try
            {
                return (T)result.ReturnValue;
            }
            catch
            {
                return defaultValue;
            }
        }
        #endregion
    }
}